package mk.gamemap.view
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import game.tileMap.MapConst;
	
	import mk.gamemap.view.anime.AnimeClip;
	import mk.gamemap.view.anime.BitmapAnime;
	import mk.gamemap.view.anime.IAnime;
	
	

	public class AnimeFactory
	{
		
		
		[Embed(source="asset/People5.png")]
		public static const embedImage:Class;
		
		
		[Embed(source="asset/Boss.png")]
		public static const embedBoss:Class;
		
		
		private var source:BitmapData
		
		
		public static var TileSize:int=32
		public function AnimeFactory()
		{
			init()
		}
		
		
		public function init():void{
			
				
			var bitmap:Bitmap=new embedImage() as Bitmap;
			source=bitmap.bitmapData
		}
		
		public function produce():Sprite{
			
			var bitMapData:BitmapData=new BitmapData(TileSize,TileSize);
			var rect:Rectangle = new Rectangle(16, 32+16, TileSize,TileSize);
			var pt:Point = new Point(0, 0);
			bitMapData.copyPixels(source,rect,pt);
			var image:Bitmap=new Bitmap(bitMapData);
			var view:Sprite=new Sprite
			view.addChild(image);
			return view;
		}
		
		
		
		public function produceBoss():AnimeClip{
			var bitmap:Bitmap=new embedBoss() as Bitmap;
			var bitMapData:BitmapData=bitmap.bitmapData
			
			var tempArr0:Array=[bitMapData]
			var anime0:IAnime=new BitmapAnime(tempArr0,"向下走");
			var arr:Array=[]
			arr.push(anime0);
			return new AnimeClip(arr);
		}
		
		
		public function produceAnime():AnimeClip{
			
			var bitMapData:BitmapData=new BitmapData(TileSize*3,TileSize*4);
			var rect:Rectangle = new Rectangle(0*32, 0*32, TileSize*3,TileSize*4);
			var pt:Point = new Point(0, 0);
			bitMapData.copyPixels(source,rect,pt);
			var allImages:Array=splitImage(bitMapData,32,32);
			
			var tempArr0:Array=allImages.slice(0,3);
			var anime0:IAnime=new BitmapAnime(tempArr0,"向下走");
			var tempArr1:Array=allImages.slice(4,7);
			var anime1:IAnime=new BitmapAnime(tempArr1,"向左走");
			var tempArr2:Array=allImages.slice(7,10);
			var anime2:IAnime=new BitmapAnime(tempArr2,"向右走");
			var tempArr3:Array=allImages.slice(10,13);
			var anime3:IAnime=new BitmapAnime(tempArr3,"向上走");
			var arr:Array=[]
			arr.push(anime0);
			arr.push(anime1);
			arr.push(anime2);
			arr.push(anime3);
			return new AnimeClip(arr);
		}
		
		public static function splitImage(source:BitmapData,width:int,height:int):Array
		{
			var arr:Array = [];
			for (var j:int = 0;j < Math.ceil(source.height / height);j++)
			{
				for (var i:int = 0;i < Math.ceil(source.width / width);i++)
				{
					var bitmapData:BitmapData = new BitmapData(width,height,true,0);
					bitmapData.copyPixels(source,new Rectangle(i*width,j*height,width,height),new Point());
					
						arr.push(bitmapData)
				}	
			}
			return arr;
		}
	}
}